package com.hefei.edu.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hefei.edu.entity.Teacher;
import com.hefei.edu.service.ITeacherService;
import com.hefei.common.utils.R;
import com.hefei.edu.vo.TeacherQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author caizhengjun
 * @since 2021-03-04
 */
@Api(tags = "讲师管理")
@RestController
@RequestMapping("/edu/teacher")
public class TeacherController {

    @Autowired
    private ITeacherService teacherService;

    @ApiOperation(value = "查询讲师列表")
    @GetMapping("/list")
    public R list() {

        List<Teacher> list = this.teacherService.list(null);

        Map<String, Object> map = new HashMap<>();
        map.put("items", list);

        return R.ok().data(map);
    }

    @ApiOperation(value = "分页条件查询讲师列表")
    @PostMapping("/{page}/{size}")
    public R pageList(
            @ApiParam(name = "page", value = "当前页", required = true) @PathVariable Integer page,
            @ApiParam(name = "size", value = "页大小", required = true) @PathVariable Integer size,
            @ApiParam(name = "TeacherQueryVo", value = "查询VO") @RequestBody TeacherQueryVo teacherQueryVo) {

        Map<String, Object> map = this.teacherService.pageList(page, size, teacherQueryVo);

        return R.ok().data(map);
    }

    @ApiOperation(value = "添加讲师")
    @PostMapping("/save")
    public R save(
            @ApiParam(name = "teacher", value = "讲师")
            @RequestBody Teacher teacher) {

        boolean save = this.teacherService.save(teacher);
        if (save) {
            return R.ok().message("添加成功！");
        } else {
            return R.fail().message("添加失败！");
        }
    }

    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("/delete/{id}")
    public R deleteByIds(@PathVariable("id") String id) {

        boolean result = this.teacherService.removeById(id);
        if (result) {
            return R.ok().message("删除成功！");
        } else {
            return R.fail().message("删除失败！");
        }
    }

    @GetMapping("/{id}")
    public R getById(@PathVariable("id") String id) {

        Teacher teacher = this.teacherService.getById(id);
        if (!StringUtils.isEmpty(teacher)) {
            return R.ok().message("查询讲师成功").data(teacher);
        } else {
            return R.fail().message("查询讲师信息失败");
        }
    }

    @PostMapping("/update")
    public R update(@RequestBody Teacher teacher) {

        boolean result = this.teacherService.updateById(teacher);
        if (result) {
            return R.ok().message("更新成功");
        } else {
            return R.fail().message("更新失败");
        }
    }
}

